package com.yonyou.pmclouds.trialinfo.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.entity.PageObject;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.CommonProgramConsts;
import com.yonyou.pmclouds.trialinfo.entity.TrialInfoVO;
import com.yonyou.pmclouds.trialinfo.mapper.TrialInfoMapper;
import com.yonyou.pmclouds.trialinfo.rmiitf.TrialInfoQueryService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;

@Service(version = ApplicationConsts.APPLICATION_VERSION,interfaceClass = TrialInfoQueryService.class)
public class TrialInfoQueryServiceImpl implements TrialInfoQueryService {

    @Autowired
    private TrialInfoMapper mapper;

    @Override
    public TrialInfoVO queryTrialInfoById(String pkTrialInfo) {
        return mapper.getById(pkTrialInfo);
    }

    @Override
    public PageObject<TrialInfoVO> queryTrialInfoList(Byte billstatus, int start, int offset) throws BusinessException {
        if (offset > CommonProgramConsts.DEFAULT_MAX_QUERY_SIZE) {
            offset = CommonProgramConsts.DEFAULT_MAX_QUERY_SIZE;
        }
        if (offset < CommonProgramConsts.DEFAULT_MIN_QUERY_SIZE) {
            offset = CommonProgramConsts.DEFAULT_MIN_QUERY_SIZE;
        }
        int sum = mapper.countTrialInfo(billstatus);
        TrialInfoVO[] trialInfoVOs = mapper.queryTrialInfoList( billstatus, start, offset);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        for (TrialInfoVO trialInfoVO: trialInfoVOs) {
            if(trialInfoVO.getApplicationtime() != null)
                trialInfoVO.setApplicationdate(sdf.format(trialInfoVO.getApplicationtime()));
            if(trialInfoVO.getEndtime() != null)
                trialInfoVO.setEnddate(sdf.format(trialInfoVO.getEndtime()));
            if(trialInfoVO.getApprovetime() != null)
                trialInfoVO.setApprovedate(sdf.format(trialInfoVO.getApplicationtime()));
        }

        return new PageObject<>(trialInfoVOs, sum, start);
    }
}
